package com.atguigu.gulimall.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.common.vo.MemberRespVo;
import com.atguigu.gulimall.seckill.interceptor.LoginUserInterceptor;
import com.atguigu.gulimall.seckill.service.SeckillService;
import com.atguigu.gulimall.seckill.to.SeckillSkuInfoRedisTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 马迎迎
 * @Title: SeckillController
 * @Package com.atguigu.gulimall.seckill.controller
 * @Description: TODO(添加描述)
 * @date 2021年06月02日 18:35
 */
@Slf4j
@Controller
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    /**
     * 返回当前时间可以参与秒杀的商品信息
     * @param
     * @return
     */
    @ResponseBody
    @GetMapping("/currentTimeSeckillSkus")
    public R getCurrentTimeSeckillSkus(){
        log.info("方法开始执行...");
        List<SeckillSkuInfoRedisTo> list = seckillService.getCurrentTimeSeckillSkus();
        return R.ok().setData(list);
    }

    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable Long skuId){
//        try {
//            TimeUnit.SECONDS.sleep(1);
//        } catch(InterruptedException e) {
//            e.printStackTrace();
//        }
        SeckillSkuInfoRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(to);
    }

    @GetMapping("/seckill/item")
    public String seckill(@RequestParam("seckillId") String seckillId, @RequestParam("count") Integer count, @RequestParam("seckillToken") String seckillToken,
                          Model model) throws InterruptedException {
        //1.判断是否登录

        String orderSn = seckillService.seckill(seckillId,count,seckillToken);
        model.addAttribute("orderSn",orderSn);
//        return R.ok().setData(orderSn);
        return "success";
    }

}
